'''
Created on 2011-9-29

@author: leo
'''
import wx
import sys
import os
from log import log
path = os.path.dirname(sys.argv[0])
class TaskBarIcon(wx.TaskBarIcon):
    ID_Exit = wx.NewId()
    ID_Show = wx.NewId()
    def __init__(self, frame):
        wx.TaskBarIcon.__init__(self)
        self.frame = frame
        self.SetIcon(wx.Icon(name=path+'/network.png', type=wx.BITMAP_TYPE_PNG), 'Dr.com For Ubuntu')
        self.Bind(wx.EVT_TASKBAR_LEFT_DCLICK, self.OnTaskBarLeftDClick)
        self.Bind(wx.EVT_MENU, self.OnExit, id=self.ID_Exit)
        self.Bind(wx.EVT_MENU, self.OnShow, id=self.ID_Show)
    def OnTaskBarLeftDClick(self, event):
        if self.frame.IsIconized():
            self.frame.Iconize(False)
        if not self.frame.IsShown():
            self.frame.Show(True)
        self.frame.Raise()
        self.frame.taskBarIcon.Destroy()
    def OnShow(self, event):
        if self.frame.IsIconized():
            self.frame.Iconize(False)
        if not self.frame.IsShown():
            self.frame.Show(True)
        self.frame.Raise()
        self.frame.taskBarIcon.Destroy()
    def OnExit(self, event):
        if self.frame.thread != None:
            if self.frame.thread.isAlive():
                self.frame.thread.stop()
                log("thread stop")
        self.frame.taskBarIcon.Destroy()
        self.frame.Destroy()
        log("Exit\n\n")
    def CreatePopupMenu(self):
        menu = wx.Menu()
        menu.Append(self.ID_Exit, 'Exit')
        menu.Append(self.ID_Show, 'Show')
        return menu